﻿<%@ Page AutoEventWireup="true" CodeBehind="Account.aspx.cs" Inherits="AssetManage.Manage.Account" Language="C#" MasterPageFile="~/Site.Master" Title="账户管理" %>

<%@ MasterType VirtualPath="~/Site.Master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
    var SelectOperator = "#<%= SelectOperator.ClientID %>";
    var UserID = "#<%= UserID.ClientID %>";

    var TableID = "#<%= TableID.ClientID %>";
    var TableID_Error = "#<%= TableID_Error.ClientID %>";

    var RealName = "#<%= RealName.ClientID %>";
    var RealName_Error = "#<%= RealName_Error.ClientID %>";

    var Submit = "#Submit";

    $(function() {
        $(Submit).attr("disabled", "disabled");

        $("[title]").tooltip({
            position: {
                my: "center top+15",
                at: "center bottom",
                using: function(position, feedback) {
                    $(this).css(position);
                    $("<div>")
                        .addClass("arrow top")
                        .addClass(feedback.vertical)
                        .addClass(feedback.horizontal)
                        .appendTo(this);
                }
            }
        });

        $("#UserID_div").hide();
        $(TableID).val("");
        $(RealName).val("");
        $(Submit).attr("disabled", "disabled");

        $(SelectOperator).change(function() {
            switch ($(SelectOperator).find("option:selected").val()) {
            case "Add":
                $("#UserID_div").hide();
                $("#TableID_div").show();
                $("#RealName_div").show();
                $(TableID).val("");
                $(RealName).val("");
                $(Submit).attr("disabled", "disabled");
                break;
            case "Delete":
                $("#UserID_div").show();
                $("#TableID_div").hide();
                $("#RealName_div").hide();
                $(Submit).removeAttr("disabled");
                break;
            }
        });

        $(UserID).change(function() {
            $(TableID).val($(UserID).find("option:selected").val());
            $(RealName).val($(UserID).find("option:selected").text());
        });

        $(TableID).change(function() {
            switch ($(SelectOperator).find("option:selected").val()) {
            case "Add":
                $(Submit).attr("disabled", "disabled");

                if ($(TableID).val() === "") {
                    $(TableID_Error).text("请输入账户表。");
                } else {
                    $(TableID).val($(TableID).val().toUpperCase());
                    var reg = /^[A-Z0-9_]\w{1,16}$/;
                    if (!reg.test($(TableID).val())) {
                        $(TableID_Error).text("请输入正确的账户表。");
                    } else {
                        var sendData = {
                            section: "tableid",
                            tableid: $(TableID).val()
                        };
                        $.ajax({
                            type: "POST",
                            url: "/Handler/Validate",
                            data: sendData,
                            success: function(response) {
                                var responseData = eval("(" + response + ")");
                                if (responseData.exist) {
                                    $(TableID_Error).text("该账户表已存在。请输入其他的账户表。");
                                } else {
                                    $(TableID_Error).text("");
                                    if (HaveNoError()) {
                                        $(Submit).removeAttr("disabled");
                                    }
                                }
                            }
                        });
                    }
                }
                break;
            }
        });

        $(RealName).change(function() {
            switch ($(SelectOperator).find("option:selected").val()) {
            case "Add":
                $(Submit).attr("disabled", "disabled");

                if ($(RealName).val() === "") {
                    $(RealName_Error).text("请输入姓名。");
                } else {
                    var reg = /^[\u4e00-\u9fa5a-zA-Z0-9_]{2,16}$/;
                    if (!reg.test($(RealName).val())) {
                        $(RealName_Error).text("请输入正确的姓名。");
                    } else {
                        $(RealName_Error).text("");
                        if (HaveNoError()) {
                            $(Submit).removeAttr("disabled");
                        }
                    }
                }
                break;
            }
        });

        $(Submit).click(function() {
            $(Submit).attr("disabled", "disabled");

            $("#message").text("确定" + $(SelectOperator).find("option:selected").text() + "?");
            $("#success").text("");
            $("#error").text("");

            $("#dialog").dialog({
                title: "询问",
                closeText: "关闭",
                modal: true,
                resizable: false,
                buttons: {
                    "确定": function() {
                        $(Submit).attr("value", "正在等待服务器响应……");
                        $(this).dialog("close");
                        var sendData;
                        switch ($(SelectOperator).find("option:selected").val()) {
                        case "Add":
                            sendData =
                            {
                                action: "create",
                                tableid: $(TableID).val(),
                                realname: $(RealName).val()
                            };
                            break;
                        case "Delete":
                            sendData =
                            {
                                action: "delete",
                                tableid: $(UserID).val()
                            };
                            break;
                        }

                        $.ajax({
                            type: "POST",
                            url: "/Handler/Account",
                            data: sendData,
                            success: function(response) {
                                $("#message").text("");
                                var responseData = eval("(" + response + ")");
                                if (responseData.success) {
                                    $(TableID_Error).text("");
                                    $(RealName_Error).text("");
                                    $(ShowResultDialog("success"));
                                    switch ($(SelectOperator).find("option:selected").val()) {
                                    case "Add":
                                        $(UserID).append("<option value='" + $(TableID).val() + "'>" + $(RealName).val() + "</option>");
                                        $(Submit).attr("disabled", "disabled");
                                        break;
                                    case "Delete":
                                        $(UserID).find("option:selected").remove();
                                        break;
                                    }
                                    $(Submit).removeAttr("disabled");
                                } else {
                                    switch (responseData.error) {
                                    case "TableID_Error":
                                        $(TableID_Error).text("请输入账户表。");
                                        break;
                                    case "RealName_Error":
                                        $(RealName_Error).text("请输入姓名。");
                                        break;
                                    case "TableID_IsExist":
                                        $(TableID_Error).text("该账户表已存在。请输入其他的账户表。");
                                        break;
                                    case "Server_IsExist":
                                        $(ShowResultDialog("fail", $(SelectOperator).find("option:selected").text() + "错误！"));
                                        break;
                                    }
                                }
                                $(Submit).attr("value", "确定");
                            }
                        });
                    },
                    "取消": function() {
                        $(this).dialog("close");
                        $(Submit).removeAttr("disabled");
                        $(Submit).attr("value", "确定");
                    }
                }
            });
        });
    });

    function HaveNoError() {
        var result = true;
        $(".form-horizontal :text").each(function() {
            if ($(this).val() === "") {
                result = false;
            }
        });
        $("[id$='_Error']").each(function() {
            if ($(this).text() !== "") {
                result = false;
            }
        });
        return result;
    }

    var AutoLeftSeconds = 5;

    function CountDownClose() {
        if (AutoLeftSeconds <= 0) {
            $("#dialog").dialog("close");
            $("#dialog").dialog("option", "title", "正在关闭……");
            AutoLeftSeconds = 5;
            return;
        }
        $("#dialog").dialog("option", "title", "将在" + AutoLeftSeconds + "秒后自动关闭……");
        setTimeout(CountDownClose, 1000);
        AutoLeftSeconds--;
    }

    function ShowResultDialog(code, message) {
        $(function() {
            if (code === "success") {
                $("#success").text("完成!");
            } else {
                $("#error").text(message);
            }

            $("#dialog").dialog({
                title: "请稍候……",
                closeText: "关闭",
                modal: true,
                resizable: false,
                buttons: {
                    "确定": function() {
                        $(this).dialog("close");
                    }
                }
            });

            $(CountDownClose);
        });
    };
</script>

<div class="form-horizontal">
    <h3>账户管理</h3>
    <hr/>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="SelectOperator" CssClass="col-md-2 control-label">操作</asp:Label>
        <div class="col-md-10">
            <asp:DropDownList ID="SelectOperator" CssClass="form-control" runat="server" Font-Size="Medium">
                <asp:ListItem Value="Add">增加账户</asp:ListItem>
                <asp:ListItem Value="Delete">删除账户</asp:ListItem>
            </asp:DropDownList>
        </div>
    </div>
    <div class="form-group" id="UserID_div">
        <asp:Label runat="server" AssociatedControlID="UserID" CssClass="col-md-2 control-label">账户</asp:Label>
        <div class="col-md-10">
            <asp:DropDownList ID="UserID" CssClass="form-control" runat="server" DataSourceID="Account_SqlDataSource" DataTextField="RealName" DataValueField="TableID" Font-Size="Medium">
            </asp:DropDownList>
            <asp:SqlDataSource ID="Account_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Account_ConnectionString %>" ProviderName="<%$ ConnectionStrings:Account_ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Account] WHERE ([TableID] <> 'Administrator') AND ([TableID] <> 'Public')"></asp:SqlDataSource>
        </div>
    </div>
    <div id="TableID_div" class="form-group">
        <asp:Label runat="server" AssociatedControlID="TableID" CssClass="col-md-2 control-label">账户表</asp:Label>
        <div class="col-md-10">
            <asp:TextBox ID="TableID" CssClass="form-control" runat="server" Font-Size="Medium" ToolTip="账户表只能由大写英文字母、数字和下划线组成。"></asp:TextBox>
            <asp:Label ID="TableID_Error" runat="server" CssClass="text-danger"></asp:Label>
        </div>
    </div>
    <div id="RealName_div" class="form-group">
        <asp:Label runat="server" AssociatedControlID="RealName" CssClass="col-md-2 control-label">姓名</asp:Label>
        <div class="col-md-10">
            <asp:TextBox ID="RealName" ToolTip="姓名不能含有特殊符号，长度至少2位。" CssClass="form-control" runat="server" Font-Size="Medium"></asp:TextBox>
            <asp:Label ID="RealName_Error" runat="server" CssClass="text-danger"></asp:Label>
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="RealName" CssClass="col-md-2 control-label"></asp:Label>
        <div class="col-md-10">
            <input id="Submit" type="button" value="确定" class="btn btn-primary btn-lg"/>
        </div>
    </div>
</div>
<div id="dialog">
    <div id="message" style="font-size: medium"></div>
    <div id="success" class="text-success" style="font-size: medium"></div>
    <div id="error" class="text-danger" style="font-size: medium"></div>
</div>
</asp:Content>